package tools

import (
	"bytes"
	"encoding/json"
	"fmt"
	"hft/global"
	"io"
	"net/http"
	"net/url"
)

type SuccessIdCardResponse struct {
	Msg     string     `json:"msg"`
	Code    string     `json:"code"`
	Balance string     `json:"balance,omitempty"`
	Data    IdCardData `json:"data,omitempty"`
}

type IdCardData struct {
	ResponseCode string
	ResponseDesc string
	TraceNo      string
}

func ValidIDCard(idCardNo string, name string) (*SuccessIdCardResponse, error) {
	fmt.Println("s", idCardNo, name)
	params := url.Values{
		"idCardNo": []string{idCardNo},
		"name":     []string{name},
	}
	fmt.Println("1")
	reader := bytes.NewReader([]byte{})
	fmt.Println("2")
	request, err := http.NewRequest("POST", global.OC_CONFIG.IDCard.Url+"?"+params.Encode(), reader)
	fmt.Println("3")
	defer request.Body.Close()
	fmt.Println("4")
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	fmt.Println("5")
	request.Header.Set("Authorization", "APPCODE "+global.OC_CONFIG.IDCard.AppCode)
	fmt.Println("6")
	client := http.Client{}
	fmt.Println("7")
	resp, err := client.Do(request)
	fmt.Println("8")
	if err != nil {
		fmt.Println("22222", err.Error())
		return nil, err
	}
	fmt.Println("9")
	respBytes, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("33333", err.Error())
		return nil, err
	}
	fmt.Println("10")
	var resStruct *SuccessIdCardResponse
	fmt.Println("11")
	json.Unmarshal(respBytes, &resStruct)
	fmt.Println("12", JsonParse(resStruct), string(respBytes))
	return resStruct, nil
}
